/**
 * 图片上传七牛代码
 * @param {Object} win
 */
(function(win) {
	var config = {
		useCdnDomain: true,
		disableStatisticsReport: false,
		retryCount: 6,
		region: qiniu.region.z2
	};
	var putExtra = {
		fname: "",
		params: {},
		mimeType: null
	};
	//var domain = "pf1dxl2ll.bkt.clouddn.com";

	//var token = "";

	var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

	function generateMixed(n) {
		var res = "";
		for(var i = 0; i < n; i++) {
			var id = Math.ceil(Math.random() * 35);
			res += chars[id];

		}
		return res;
	}
	var uploadFile = function(file, token, putExtra, config, domain, error, complete, next, key) {
		if(file) {
			putExtra.params["x:name"] = key.split(".")[0];
			var subObject = {
				next: next,
				error: error,
				complete: complete
			};
			var subscription;
			observable = qiniu.upload(file, key, token, putExtra, config);
			observable.subscribe(subObject);
		}
	}

	/**
	 * 文件上传对象
	 */
	var obj = {};
	/**
	 * 单图片上传
	 * @param {String} input type="file" id
	 * @param {String} getConfig返回的token
	 * @param {String} getConfig返回的domain
	 * @param {Function} 成功
	 * @param {Function} 失败
	 * @param {Function} 完成
	 */
	obj.upload = function(id, token, domain, success, error, compelte) {
		var name = new Date().getTime().toString() + generateMixed(5);
//		var file = document.getElementById(id).files[0];
		var file = id;
		if(file == null) {
			if(error) error("请选择图片");
			return;
		}
		var k = file.name;
		var arr = k.split(".");
		var key = name + "." + arr[arr.length - 1];

		uploadFile(file, token, putExtra, config, domain, function(err) {
			if(error)
				error("上传出错");
		}, function(path) {
			success(key);
		}, function(a) {}, key);
	}

	/**
	 * 上传多张图片
	 * @param {Array} id 控件ID,name 对应字段名,error 错误信息,nil 空信息
	 * @param {String} Token
	 * @param {String} success
	 * @param {Function} success
	 * @param {Function} error
	 * @param {Function} compelte
	 */
	obj.uploads = function(files, token, domain, success, error, compelte) {
		for(var i = 0; i < files.length; i++) {
			var id = files[i].id;
			console.log(id);
			var file = document.getElementById(id).files[0];
			if(file == null) {
				if(error) error(files[i].nil);
				return;
			}
		}

		var count = files.length;
		var result = {
			data: [],
			success: 0,
			error: 0,
		};
var self=this;
		function up(id, name) {
			self.upload(files[i].id,token,domain, function(path) {
				result.data.push({
					name: name,
					value: path,
					success: true
				});
				result.success++;
				count--;
				if(count == 0) {
					success(result);
				}
			}, function(err) {
				result.data.push({
					name: name,
					value: err,
					success: false
				});
				result.error++;
				count--;
				if(count == 0) {
					success(result);
				}
			});
		}
		for(var i = 0; i < files.length; i++) {
			up(files[i].id, files[i].name);
		}
	}
	/**
	 * 加载上传的配置文件
	 * @param {Function} 成功，token,domain
	 * @param {Function} 请求失败
	 */
	obj.getConfig = function(success, error) {
		$.ajax({
			type: "POST",
			url: RequestAddress + "/System/upload",
			data: {},
			beforeSend: function(xhr) {
				token = getItem("Userdata").token;
				xhr.setRequestHeader("Authorization", "Basic " + token);
			},
			success: function(data) {
				//						console.log(JSON.stringify(data))					
				if(data.status == 1) {
					success(data.data.token, data.data.domain);
				} else {
					error("请求错误");
				}
			},
			error: function(data) {
				console.log(JSON.stringify(data));
			}
		});
	}
	/*
	 * 文件上传对象
	 */
	win.file = obj;
})(window);

//demo
/*
file.getConfig(function(token, domain) {
	file.upload("file", token, domain, function(path) {
		//上传成功
	}, function(error) {
		console.log(error);
	});
}, function(err) {
	console.log(err);
});
*/

//file.getConfig(function(token,domain){
//	var files=[];
//	files.push({id:"",name:""});
//	files.push({id:"",name:""});
//	files.push({id:"",name:""});
//	
//	
//	fiel.uploads(files,token,domain,function(path){
//		
//	},function(err){
//		
//	});
//},function(){
//	
//});